package com.kgc.warehouse.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.warehouse.system.common.CodeMsg;
import com.kgc.warehouse.system.common.ResponseResult;
import com.kgc.warehouse.system.entity.Permission;
import com.kgc.warehouse.system.entity.RolePermission;
import com.kgc.warehouse.system.mapper.PermissionMapper;
import com.kgc.warehouse.system.mapper.RolePermissionMapper;
import com.kgc.warehouse.system.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author main
* @description 针对表【sys_permission】的数据库操作Service实现
* @createDate 2023-02-03 20:07:42
*/
@Service
@Slf4j
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>
    implements PermissionService{

    @Autowired(required = false)
    private PermissionMapper permissionMapper;

    @Autowired(required = false)
    private RolePermissionMapper rolePermissionMapper;
    @Override
    public ResponseResult add(Permission permission) {


        log.info("====="+permission);
        String type = permission.getType();
       if (type.equals("menu")&&permission.getPercode().equals(""))
       {
           //查询此项功能在相同目录下是否存在
           Integer parentId = permission.getParentId();
           LambdaQueryWrapper<Permission> permissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
           permissionLambdaQueryWrapper.eq(Permission::getTitle,permission.getTitle())
                   .eq(Permission::getParentId,parentId);
           List<Permission> permissions = permissionMapper.selectList(permissionLambdaQueryWrapper);
           if (permissions.size()>0)
           {
               return new ResponseResult(CodeMsg.PERMISSION_EXIT);
           }
           permissionMapper.insert(permission);
           return new ResponseResult(CodeMsg.SUCCESS);
       }else if (type.equals("permission")&&permission.getHref().equals(""))
       {
           //查询此项功能在相同目录下是否存在
           Integer parentId = permission.getParentId();
           LambdaQueryWrapper<Permission> permissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
           permissionLambdaQueryWrapper.eq(Permission::getTitle,permission.getTitle())
                   .eq(Permission::getParentId,parentId);
           List<Permission> permissions = permissionMapper.selectList(permissionLambdaQueryWrapper);
           if (permissions.size()>0)
           {
               return new ResponseResult(CodeMsg.PERMISSION_EXIT);
           }
           permissionMapper.insert(permission);
           return new ResponseResult(CodeMsg.SUCCESS);
       }

       return new ResponseResult(CodeMsg.PERMISSION_ERRO);

//        //查询此项功能在相同目录下是否存在
//        Integer parentId = permission.getParentId();
//        LambdaQueryWrapper<Permission> permissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        permissionLambdaQueryWrapper.eq(Permission::getTitle,permission.getTitle())
//        .eq(Permission::getParentId,parentId);
//        List<Permission> permissions = permissionMapper.selectList(permissionLambdaQueryWrapper);
//        if (permissions.size()>0)
//        {
//            return new ResponseResult(CodeMsg.PERMISSION_EXIT);
//        }
//        permissionMapper.insert(permission);
//        return new ResponseResult(CodeMsg.SUCCESS);
    }

    @Override
    public ResponseResult updatePermission(Permission permission) {
        String type = permission.getType();
        log.info("====="+permission);
        if (type.equals("menu")&&permission.getPercode().equals(""))
        {
            //查询此项功能在相同目录下是否存在
            Integer parentId = permission.getParentId();
            LambdaQueryWrapper<Permission> permissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
            permissionLambdaQueryWrapper.eq(Permission::getTitle,permission.getTitle())
                    .eq(Permission::getParentId,parentId);
            List<Permission> permissions = permissionMapper.selectList(permissionLambdaQueryWrapper);
            if (permissions.size()>0)
            {
                return new ResponseResult(CodeMsg.PERMISSION_EXIT);
            }
            permissionMapper.updateById(permission);
            return new ResponseResult(CodeMsg.SUCCESS);
        }else if (type.equals("permission")&&permission.getHref().equals(""))
        {
            //查询此项功能在相同目录下是否存在
            Integer parentId = permission.getParentId();
            LambdaQueryWrapper<Permission> permissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
            permissionLambdaQueryWrapper.eq(Permission::getTitle,permission.getTitle())
                    .eq(Permission::getParentId,parentId);
            List<Permission> permissions = permissionMapper.selectList(permissionLambdaQueryWrapper);
            if (permissions.size()>0)
            {
                return new ResponseResult(CodeMsg.PERMISSION_EXIT);
            }
            permissionMapper.updateById(permission);
            return new ResponseResult(CodeMsg.SUCCESS);
        }

        return new ResponseResult(CodeMsg.PERMISSION_ERRO);


    }

    @Override
    public ResponseResult delete(Integer id) {


        //查询是否有子菜单
        LambdaQueryWrapper<Permission> permissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        permissionLambdaQueryWrapper.eq(Permission::getParentId,id);
        List<Permission> permissions = permissionMapper.selectList(permissionLambdaQueryWrapper);
        if (permissions.size()>0)
        {
            return new ResponseResult(CodeMsg.PERMISSION_USED);
        }

        //删除权限
        permissionMapper.deleteById(id);

        //删除角色权限表
        LambdaQueryWrapper<RolePermission> rolePersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        rolePersLambdaQueryWrapper.eq(RolePermission::getPid,id);
        rolePermissionMapper.delete(rolePersLambdaQueryWrapper);
        return new ResponseResult(CodeMsg.SUCCESS);
    }
}




